package com.suyuan.service.impl;

import com.suyuan.dao.CustomAddrMapper;
import com.suyuan.dao.CustomInfoMapper;
import com.suyuan.dao.CustomerCartMapper;
import com.suyuan.entity.*;
import com.suyuan.params.RegistParam;
import com.suyuan.service.CustomerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by yang on 2018/4/1.
 */
@Service(value = "customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService{
    @Resource
    private CustomAddrMapper customAddrMapper;
    @Resource
    private CustomInfoMapper customInfoMapper;
    @Resource
    private CustomerCartMapper customerCartMapper;

    public List<CustomAddr> findAddrById(Integer id) {
        return customAddrMapper.findAddrById(id);
    }

    public boolean newAddr(CustomAddr addr) {
        int code = 0;
        try {
            System.out.println("service :"+addr.getCusId());
            //code = customAddrMapper.insertSelective(addr);
            code = customAddrMapper.insertAddr(addr.getCusId(),addr.getAddr(),addr.getReceName(),addr.getRecePhone());
        } catch (Exception e) {
            code = 0;
            e.printStackTrace();
        }
        if(code == 1){
            return true;
        }
        return false;
    }

    public CustomInfo findById(Integer id) {
        return customInfoMapper.selectByPrimaryKey(id);
    }

    public CustomInfo findByUsername(String username) {
        CustomInfoExample example = new CustomInfoExample();
        CustomInfoExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<CustomInfo> infos = customInfoMapper.selectByExample(example);
        if(infos.size() > 0 && infos != null){
            return infos.get(0);
        }
        return null;
    }

    /**
     * 注册用户
     * @param param
     */
    public void registCustomer(RegistParam param) throws Exception{
        CustomInfo customInfo = new CustomInfo();
        customInfo.setUsername(param.getUsername());
        customInfo.setPassword(param.getPassword());
        customInfo.setContactsphone(param.getContactsPhone());
        customInfo.setCustomsex(param.getCustomSex());
        customInfo.setCustname(param.getCustName());
        customInfo.setContactsmail(param.getContactsMail());
        customInfo.setCustaddr(param.getCustAddr());
        Integer gid = customInfoMapper.insertSelective(customInfo);
        CustomerCart customerCart = new CustomerCart();
        customerCart.setCustomerId(gid);
        customerCart.setTotal(BigDecimal.ZERO);
        customerCartMapper.insertSelective(customerCart);
    }

    public boolean updateAddr(CustomAddr addr) {
        int code = customAddrMapper.updateAddr(addr);
        if(code == 1){
            return true;
        }
        return false;
    }
    public boolean deleteAddr(Integer id) {
        int code = customAddrMapper.deleteAddr(id);
        if(code == 1){
            return true;
        }
        return false;
    }


    /**
     * 商城后台查找所有用户
     * @return
     */
    public List<CustomInfo> findAllCustomer() {
        return customInfoMapper.selectByExample(null);
    }


    /**
     * 商城后台根据用户ID删除客户
     * @param id
     */
    public void deleteCustomer(Integer id) {
        customInfoMapper.deleteByPrimaryKey(id);
    }


    /**
     * 更新用户信息
     * @param customeInfo
     */
    public void editCustomer(CustomInfo customeInfo) {
        CustomInfo editCustomInfo = customInfoMapper.selectByPrimaryKey(customeInfo.getGid());
        if(StringUtils.isNotBlank(customeInfo.getCustname())){
            editCustomInfo.setCustname(customeInfo.getCustname());
        }
        if(StringUtils.isNotBlank(customeInfo.getContactsmail())){
            editCustomInfo.setContactsmail(customeInfo.getContactsmail());
        }
        if(StringUtils.isNotBlank(customeInfo.getCustaddr())){
            editCustomInfo.setCustaddr(customeInfo.getCustaddr());
        }
        if(StringUtils.isNotBlank(customeInfo.getContactsphone())){
            editCustomInfo.setContactsphone(customeInfo.getContactsphone());
        }
        editCustomInfo.setCustomsex(customeInfo.getCustomsex());
        editCustomInfo.setCustlevel(customeInfo.getCustlevel());
        customInfoMapper.updateByPrimaryKeySelective(editCustomInfo);
    }


}
